﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using JLL.China.Application;
using JLL.China.Web.Models;
using JLL.China.Application.Services.Authentication;
using JLL.China.Application.Services.Membership;
using JLL.China.Infrastructure.Services;
using JLL.China.Web.Models;
using JLL.China.Web.Libraries;
using System.Web.Configuration;
namespace JLL.China.Web.Controllers
{
    #region "DATA"
    public class PostModel
    {
        public int ID { get; set; }
        public string contentReject { get; set; }
    }
    #endregion
    public class HomeController : BaseController
    {
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }
        protected int defaultPageSize = Int32.Parse(WebConfigurationManager.AppSettings["page_size_default"]);
        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AdminMembershipService(); }

            base.Initialize(requestContext);
        }

       

        #region -- Login --

        public ActionResult Login()
        {
            if (Request.IsAuthenticated)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View(new LogOnModel());
            }
        }

        [HttpPost]
        public ActionResult Login(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, false);
                    var acc = AdminService.GetAdmin(model.UserName);
                    this.Session[Constants.Session_Role] = acc.Role;
                    this.Session[Constants.Session_User] = acc;
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                        return RedirectToAction("Index");
                }
                else
                {
                    ViewData["ErrorDetails"] = "Login fail. Please try again.";
                }
            }
            return View(model);
        }

        public ActionResult LogOff()
        {
            FormsService.SignOut();
            this.Session.Remove(Constants.Session_Role);
            this.Session.Remove(Constants.Session_User);
            return RedirectToAction("Login");
        }
        #endregion

        //
        // GET: /Home/

        //Dasboard
        [Authorize]
        public ActionResult Index(string Module,int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;
            var model = new HomeModel();
            if (Module == "Reject")
            {
                model.pageSizeReject = defaultPageSize;
                model.currentPageIndexReject= currentPageIndex;
                Session["currentPageIndexReject"] = currentPageIndex;
                //Pending
                model.pageSizePending = defaultPageSize;

                model.currentPageIndexPending = (Session["currentPageIndexPending"] != null) ? Int32.Parse(Session["currentPageIndexPending"].ToString()) : 1;//or session
            }
            else // Default for pending
            {
                model.pageSizePending = defaultPageSize;
                model.currentPageIndexPending = currentPageIndex;
                Session["currentPageIndexPending"] = currentPageIndex;
                //reject
                model.pageSizeReject = defaultPageSize;
                model.currentPageIndexReject = (Session["currentPageIndexReject"] != null) ? Int32.Parse(Session["currentPageIndexReject"].ToString()) : 1;//or session
            }
            
           
            return View(model);
        }
        //Change to Rejeted
        public ActionResult Rejected(int id)
        {
            try
            {

                ListingService.ChangeStatus(id.ToString(), 5);

                Session["message"] = "Status updated successfully.";
               
                    return RedirectToAction("Index");
               

            }
            catch
            {
                return View();
            }
        }
        //Change 
        public ActionResult Approve(int id)
        {
            try
            {

                ListingService.ChangeStatus(id.ToString(), 1);

                Session["message"] = "Status updated successfully.";
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        [HttpPost]
        public JsonResult RejectAndSendEmail(PostModel post)
        {
            // return status message 
            //Chang status
            ListingService.ChangeStatus(post.ID.ToString(), 5);

            //Send Email

            return Json("Success"); ;
        }
       
        //Profile
        public ActionResult Profile()
        {
            return View();
        }
        

    }
}
